package repository

import (
	"go.uber.org/zap"
	"mon-go-web/internal/database"
	"mon-go-web/internal/models"
	"mon-go-web/pkg/logger"
)

type ArticleRepository struct{}

func NewArticleRepository() *ArticleRepository {
	return &ArticleRepository{}
}

func (ar *ArticleRepository) Create(article *models.Article) int64 {
	tx := database.DB.Create(article)
	if tx.Error != nil {
		logger.Logger.Error("Error creating article", zap.Error(tx.Error))
	}
	return tx.RowsAffected
}

func (ar *ArticleRepository) GetAll() []models.Article {
	var articles []models.Article
	tx := database.DB.Find(&articles)
	if tx.Error != nil {
		logger.Logger.Error("Error retrieving articles", zap.Error(tx.Error))
	}
	return articles
}
